package com.cangmaomao.lib.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

/**
 * Author:帅气的potato
 */

public class BitMaps {

    public static Bitmap getBitMaps(ImageView imageView) {
        imageView.setDrawingCacheEnabled(true);
        Log.e("bitmap", "bitmap " + imageView.getDrawingCache());
        Bitmap bitmap2 = imageView.getDrawingCache();
        Bitmap bitmap = Bitmap.createBitmap(bitmap2);
        imageView.setDrawingCacheEnabled(false);
        return bitmap;
    }

    public static Bitmap captureView(View view) throws Throwable {
        Bitmap bm = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        view.draw(new Canvas(bm));
        return bm;
    }

    public static Bitmap captureWindowView(Activity activity) {
        View decorView = activity.getWindow().getDecorView();
        decorView.setDrawingCacheEnabled(true);
        decorView.buildDrawingCache();
        //获取屏幕整张图片
        return decorView.getDrawingCache();
    }

    public static Bitmap createBitmap(Bitmap bitmap, int x, int y, int w, int h) {
        if (bitmap != null) {
            return Bitmap.createBitmap(bitmap, x, y, w, h);
        }
        return null;
    }
}
